﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace HFController
{
    /// <summary>
    /// 提问业务逻辑
    /// </summary>
    public class AskController:ControllerBase
    {
        //单列
        private static AskController _askController;
        //实例化单列
        public static AskController getController()
        {
            if (_askController == null)
            {
                _askController = new AskController();
            }
            return _askController;
        }
        /// <summary>
        /// 提问数据库访问
        /// </summary>
        hf.DAL.Ask AskDAL = new hf.DAL.Ask();

        /// <summary>
        /// 根据ID 得到一条提问列
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.Ask getAskModel(int ID)
        {
            hf.Model.Ask model = new hf.Model.Ask();
            int total;

            List<hf.Model.Ask> list = AskDAL.GetAskList(1, 1, "ID=" + ID.ToString(), out total);
            this.BindParameter(list);
            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 绑定问答的动态参数
        /// </summary>
        /// <param name="list"></param>
        private void BindParameter(List<hf.Model.Ask> list)
        {
            BagePages bp = new BagePages();
            foreach (hf.Model.Ask ask in list)
            {
                ask.CityName = bp.getCityName(ask.CityID.ToString());
                ask.ProvinceName = bp.getProvinceName(ask.ProvinceID.ToString());
                ask.timeString = bp.TimeDifference(DateTime.Now, ask.Addtime);
                ask.BigClassName = LegalAffairsController.getController().getLegalAffairsModel(ask.ClassID).LegalAffairsTitle;
                ask.SmallClassName = LegalAffairsController.getController().getLegalAffairsBiglClassModel(ask.SmallClassID).ClassName;
                ask.LaywerName = "要修改";
                hf.Model.User userModel = UserController.getController().getUserModel(ask.UserID);
                if (userModel != null)
                {
                    ask.UserName = UserController.getController().getUserModel(ask.UserID).UID;
                }
                else {
                    ask.UserName = "未知用户";
                }
                //AnswerController.getController().getAnswerList(1 ,
                //ask.LaywerName = LawyerController.getController().getLawyerModel(ask.
            }
        }

        /// <summary>
        /// 根据答案，得到问题列表
        /// </summary>
        /// <param name="LoadCount"></param>
        /// <param name="answerList"></param>
        /// <returns></returns>
        public List<hf.Model.Ask> getAskList(int LoadCount, List<hf.Model.Answer> answerList)
        {
            List<hf.Model.Ask> list = new List<hf.Model.Ask>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "";
            string strTempSql = "";
            foreach (hf.Model.Answer an in answerList)
            {
                if (strTempSql == "")
                {
                    strTempSql = an.AskID.ToString();
                }
                else
                {
                    strTempSql = strTempSql + "," + an.AskID.ToString();
                }
            }
            if (strTempSql != "")
            {
                strSql = "ID in (" + strTempSql + ")";
            }

            //获得列表
            list = AskDAL.GetAskList(LoadCount, 1, strSql, out total);

            return list;
        }

        /// <summary>
        /// 设置最佳问题
        /// </summary>
        /// <param name="AnswerID"></param>
        /// <param name="AskID"></param>
        /// <returns></returns>
        public HFModel.ResultModel SetBestAnswer(int AnswerID ,int AskID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            BagePages bp = new BagePages();
            hf.Model.User user = bp.getLoginAccount();
            if (user == null)
            {
                result.isTrue = false;
                result.Message = "请先登录！";
            }
            else {
                hf.Model.Ask askmodel = AskController.getController().getAskModel(AskID);
                if (askmodel.UserID != user.ID)
                {
                    result.isTrue = false;
                    result.Message = "不是自己提交的问题不能设置最佳答案";
                }
                else {
                    hf.Model.Answer answer = AnswerController.getController().getAnswerModel(AnswerID);
                    if (answer.AskID != AskID)
                    {
                        result.isTrue = false;
                        result.Message = "答案和问题不匹配";
                    }
                    else {
                        try
                        {
                            askmodel.StatusID = 1;
                            new hf.DAL.Ask().Update(askmodel);
                            answer.isBest = 1;
                            new hf.DAL.Answer().Update(answer);

                            hf.Model.Lawyer lawyer = LawyerController.getController().getLawyerModel(answer.LawyerID);
                            lawyer.Point += askmodel.Points;
                            user.Point -= askmodel.Points;

                            new hf.DAL.User().Update(user);
                            new hf.DAL.Lawyer().Update(lawyer);

                            result.isTrue = true;
                            result.Message = "设置成功";
                        }
                        catch (Exception err)
                        {
                            result.isTrue = false;
                            result.Message = err.Message;
                        }
                    }
                }
            }
            return result;
        }

        public HFModel.ResultModel doModifyAsk(int ID, int StatusID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Ask askmodel = AskController.getController().getAskModel(ID);
            askmodel.StatusID = StatusID;
            try {
                new hf.DAL.Ask().Update(askmodel);
                result.isTrue = true;
                result.Message = "问题已关闭";
            }
            catch (Exception err){
                result.isTrue = false;
                result.Message = err.Message;
            }
            return result;
        }

        /// <summary>
        /// 得到提问列表，用作绑定下拉框（方法名和下面的一样，这个是重载）
        /// </summary>
        /// <param name="LoadCount">读取记录数</param>
        /// <returns></returns>
        public List<hf.Model.Ask> getAskList(int LoadCount)
        {
            List<hf.Model.Ask> list = new List<hf.Model.Ask>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = AskDAL.GetAskList(LoadCount, 1, strSql, out total);
            this.BindParameter(list);
            return list;
        }

        

        /// <summary>
        /// 得到列表，前台搜索用
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageNumber"></param>
        /// <param name="strWhere"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<hf.Model.Ask> getAskList(int PageSize, int PageNumber, string strWhere, out int total, string orderKey)
        {
            List<hf.Model.Ask> list = new List<hf.Model.Ask>();

            //获得列表
            list = AskDAL.GetAskList(PageSize, PageNumber, strWhere, out total, orderKey);
            this.BindParameter(list);
            return list;
        }

        /// <summary>
        /// 得到最新已解决的问题列表
        /// </summary>
        /// <param name="Count"></param>
        /// <param name="StatusID"></param>
        /// <returns></returns>
        public List<hf.Model.Ask> getAskList(int Count, int StatusID)
        {
            List<hf.Model.Ask> list = new List<hf.Model.Ask>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "StatusID = " + StatusID.ToString();

            //获得列表
            list = AskDAL.GetAskList(Count, 1, strSql, out total);
            this.BindParameter(list);
            return list;
        }

        /// <summary>
        /// 按关键字排序
        /// </summary>
        /// <param name="Count"></param>
        /// <param name="Key"></param>
        /// <returns></returns>
        public List<hf.Model.Ask> getAskList(int Count ,string Key)
        {
            List<hf.Model.Ask> list = new List<hf.Model.Ask>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = AskDAL.GetAskList(Count, 1, strSql, out total, Key);
            this.BindParameter(list);
            return list;
        }

        /// <summary>
        /// 得到提问列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.Ask> getAskList(out string PagerStr)
        {
            List<hf.Model.Ask> list = new List<hf.Model.Ask>();

            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = AskDAL.GetAskList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }
        /// <summary>
        /// 发布提问
        /// </summary>
        /// <param name="Title"></param>
        /// <param name="ClassID"></param>
        /// <param name="SmallClassID"></param>
        /// <param name="Content"></param>
        /// <param name="AnswerCount"></param>
        /// <param name="UserID"></param>
        /// <param name="Points"></param>
        /// <param name="ProvinceID"></param>
        /// <param name="CityID"></param>
        /// <param name="StatusID"></param>
        /// <param name="isIndex"></param>
        public void doAddAsk(string Title, int ClassID, int SmallClassID, string Content, int AnswerCount,int UserID, int Points, int ProvinceID, int CityID, int StatusID, int isIndex)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Ask model = new hf.Model.Ask();

            model.Title = Title;
            model.ClassID = ClassID;
            model.SmallClassID = SmallClassID;
            model.Content = Content;
            model.AnswerCount = AnswerCount;
            model.Addtime = DateTime.Now;

            model.EndTime = DateTime.Now.AddDays(7);
            model.UserID = UserID;
            model.Points = Points;
            model.ProvinceID = ProvinceID;
            model.CityID = CityID;
            model.StatusID = StatusID;
            model.isIndex = isIndex;

            try
            {
                model.ID = AskDAL.Add(model);

                result.isTrue = true;
                result.Message = "发布成功！";
            //    result.goUrl = "location='ask.aspx';";     待定
                result.historyUrl = "history.go(-1)";
            }
            catch(Exception err)
            {
                result.isTrue = false;
                result.Message = err.Message;
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 发布问题
        /// </summary>
        /// <param name="Title"></param>
        /// <param name="ClassID"></param>
        /// <param name="SmallClassID"></param>
        /// <param name="Content"></param>
        /// <param name="AnswerCount"></param>
        /// <param name="Points"></param>
        /// <param name="ProvinceID"></param>
        /// <param name="CityID"></param>
        public HFModel.ResultModel doAddAsk(string Title, int ClassID, int SmallClassID, string Content, int Points, int ProvinceID, int CityID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            BagePages bp = new BagePages();

            hf.Model.User user = bp.getLoginAccount();
            if (user != null)
            {
                hf.Model.Ask model = new hf.Model.Ask();
                model.Title = Title;
                model.ClassID = ClassID;
                model.SmallClassID = SmallClassID;
                model.Content = Content;
                model.AnswerCount = 0;
                model.Addtime = DateTime.Now;
                model.EndTime = DateTime.Now.AddDays(7);
                model.UserID = user.ID;
                model.Points = Points;
                model.ProvinceID = ProvinceID;
                model.CityID = CityID;
                model.StatusID = 0;
                model.isIndex = 0;

                try
                {
                    model.ID = AskDAL.Add(model);
                    result.isTrue = true;
                    result.Message = "感谢您，祝您尽快解决问题！";
                    //    result.goUrl = "location='ask.aspx';";     待定
                    result.historyUrl = "history.go(-1)";
                }
                catch (Exception err)
                {
                    result.isTrue = false;
                    result.Message = "系统问题，清联系管理员";
                    result.goUrl = "history.go(-1)";
                    result.historyUrl = "history.go(-1)";
                }
            }
            else { 
                //没登录
                result.isTrue = false;
                result.Message = "没登录！";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }

            return result;
        }

        /// <summary>
        /// 修改提问
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Title"></param>
        /// <param name="ClassID"></param>
        /// <param name="SmallClassID"></param>
        /// <param name="Content"></param>
        /// <param name="AnswerCount"></param>
        /// <param name="UserID"></param>
        /// <param name="Points"></param>
        /// <param name="ProvinceID"></param>
        /// <param name="CityID"></param>
        /// <param name="StatusID"></param>
        /// <param name="isIndex"></param>
        public void doModifyAsk(int ID, string Title, int ClassID, int SmallClassID, string Content, int AnswerCount, int UserID, int Points, int ProvinceID, int CityID, int StatusID, int isIndex)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Ask model = this.getAskModel(ID);

            model.Title = Title;
            model.ClassID = ClassID;
            model.SmallClassID = SmallClassID;
            model.Content = Content;
            model.AnswerCount = AnswerCount;
            model.UserID = UserID;
            model.Points = Points;
            model.ProvinceID = ProvinceID;
            model.CityID = CityID;
            model.StatusID = StatusID;
            model.isIndex = isIndex;
        }

        /// <summary>
        /// 删除提问
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeAsk(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            try
            {
                AskDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='AskList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="IDS"></param>
        public void doDelegeAsk(string[] IDS)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                for (int i = 0; i < IDS.Length; i++)
                {
                    AskDAL.Delete(int.Parse(IDS[i].Trim()));
                }
                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='AskList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
    }
}
